package com.angel.service.ServiceImpl;

import com.angel.bean.VipCard;
import com.angel.bean.VipInfo;
import com.angel.service.VipCardService;
import com.angel.service.VipInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.angel.bean.UserInfo;
import com.angel.mapper.UserInfoDao;
import com.angel.service.UserInfoService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private UserInfoDao userInfoDao;

    @Resource
    private VipInfoService vipInfoService;

    @Resource
    private VipCardService vipCardService;

    @Override
    public int insert(UserInfo userInfo) {
        return userInfoDao.insert(userInfo);
    }

    @Override
    public int insertSelective(UserInfo userInfo) {
        return userInfoDao.insertSelective(userInfo);
    }

    @Override
    public int insertList(List<UserInfo> userInfo) {
        return userInfoDao.insertList(userInfo);
    }

    @Override
    public int update(UserInfo userInfo) {
        return userInfoDao.update(userInfo);
    }


    @Override
    public List<UserInfo> findAll(UserInfo userInfo) {
        return userInfoDao.findAll(userInfo);
    }

    @Override
    public UserInfo findOne(UserInfo userInfo) {
        List<UserInfo> list = this.findAll(userInfo);
        if (list == null || list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    @Override
    public List<UserInfo> searchUserInfo(UserInfo userInfo) {
        return userInfoDao.searchUserInfo(userInfo);
    }

    @Override
    public int delUserInfo(UserInfo userInfo) {
        return userInfoDao.delUserInfo(userInfo);
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public int updateUserInfo(UserInfo userInfo, VipInfo vipInfo) {
        VipInfo tmp = new VipInfo();
        tmp.setUserId(userInfo.getId());
        tmp = vipInfoService.findOne(tmp);
        int result = 0;
        int result1 = 0;
        try {
            if (tmp == null) {
                //新增的会员信息
                VipCard vipCard = new VipCard();
                vipCard.setVipId(vipInfo.getVipId());
                vipCard = vipCardService.findOne(vipCard);
                vipInfo.setUserId(userInfo.getId());
                vipInfo.setBalance(vipCard.getChargeClaim());
                vipInfo.setRemainTime(vipCard.getSumTime());
                vipInfo.setState(1);
                vipInfo.setChargeDtae(new Timestamp(System.currentTimeMillis()));
                result = vipInfoService.insert(vipInfo);
            } else {
                vipInfo.setUserId(userInfo.getId());
                //更新的会员信息
                result = vipInfoService.update(vipInfo);
            }
            result1 = userInfoDao.update(userInfo);

        } catch (Exception ep) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ep.printStackTrace();
            try {
                throw ep;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (result != 0 || result1 != 0) {
            return 1;
        }else {
            return 0;
        }

    }
}
